package com.frxuexi.user.view

import android.text.TextUtils
import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.room.User
import com.frxuexi.user.intent.UserIntent
import com.frxuexi.user.viewmodel.UserViewModel
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

@Composable
fun LoginPage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect {
                when (it) {
                    is IUiState.onSuccess<*> -> {
                        Log.i("===", it.data.toString())
                        ToastUtils.showLong("登录成功")
                        val user = it.data as User
                        val kv = MMKV.defaultMMKV()
                        kv.encode("uid",user.uid!!)
                        kv.encode("uname",user.uname)
                        kv.encode("role",user.role)
                        navHostController.popBackStack()
                    }

                    is IUiState.onError -> {
                        Log.i("===", "登录失败")
                    }

                    else -> {}
                }
            }
        }
    })

    var uname by remember {
        mutableStateOf("")
    }
    var upass by remember {
        mutableStateOf("")
    }

    var role by remember {
        mutableStateOf(2)
    }

    Column {
        MyTitleBar(navHostController = navHostController, title = "登录")
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(20.dp)) {
            OutlinedTextField(
                value = uname,
                onValueChange = { uname = it },
                label = { Text(text = "用户名") },
                modifier = Modifier.fillMaxWidth()
            )
            OutlinedTextField(value = upass, onValueChange = { upass = it },
                visualTransformation = PasswordVisualTransformation(),
                label = { Text(text = "密码") }, modifier = Modifier.fillMaxWidth()
            )
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "角色:")
                Spacer(modifier = Modifier.width(20.dp))
                Checkbox(checked = role == 1, onCheckedChange = {
                    if (it) {
                        role = 1
                    }
                })
                Spacer(modifier = Modifier.width(5.dp))
                Text(text = "经理")
                Spacer(modifier = Modifier.width(20.dp))
                Checkbox(checked = role == 2, onCheckedChange = {
                    if (it) {
                        role = 2
                    }
                })
                Spacer(modifier = Modifier.width(5.dp))
                Text(text = "员工")
            }
            Button(onClick = {
                if (TextUtils.isEmpty(uname) || TextUtils.isEmpty(upass) ) {
                    ToastUtils.showLong("输入内容不能为空")
                    return@Button
                }
//                if(RegexUtils.isUsername(uname))
//                if(uname.matches(Regex("1[0-9_]{10}"))){
                if (!uname.matches(Regex("[A-z0-9_]{3,9}"))) {
                    ToastUtils.showLong("不是合法用户名")
                    return@Button
                }
                vm.sendIntent(UserIntent.login(uname = uname, pass = upass, role = role))
            }, modifier = Modifier.fillMaxWidth()) {
                Text(text = "登录")
            }

            Text(text = "去注册", color = Color.Blue, modifier =  Modifier.clickable {
                navHostController.navigate(RouterPath.REGISTER_PAGE)
            })
        }

    }
}